package com.taoge.p4.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taoge.p4.common.constants.RedisKeyConstant;
import com.taoge.p4.common.constants.SysUserContext;
import com.taoge.p4.common.constants.SysUserInfo;
import com.taoge.p4.common.constants.UserContext;
import com.taoge.p4.common.entity.Brand;
import com.taoge.p4.common.entity.ResponseData;
import com.taoge.p4.common.params.BrandParam;
import com.taoge.p4.common.params.PageParam;
import com.taoge.p4.common.rabbit.exchange.RabbitmqExchange;
import com.taoge.p4.common.rabbit.msg.BrandProducterMsg;
import com.taoge.p4.common.rabbit.producer.RabbitmqProducer;
import com.taoge.p4.common.rabbit.routingKey.BrandRoutingKey;
import com.taoge.p4.common.service.BrandService;
import com.taoge.p4.common.vo.BrandHistoryVO;
import com.taoge.p4.common.vo.BrandVO;
import io.netty.util.concurrent.ThreadPerTaskExecutor;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/sys/brand")
public class BrandController {

    private final BrandService brandService;

    private final StringRedisTemplate stringRedisTemplate;

    private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

    private final RabbitmqProducer rabbitmqProducer;

    /**
     * 新增或者编辑
     *
     * @param param
     * @return
     */
    @PostMapping("/edit")
    public ResponseData<?> edit(@RequestBody BrandParam param) {
        brandService.edit(param);
        //stringRedisTemplate.convertAndSend(RedisChannelEnum.BRAND_REFRESH_CHANNEL.getChannel(), RedisKeyConstant.REFRESH);
        return ResponseData.success();
    }

    /**
     * 修改品牌状态
     *
     * @param id
     * @return
     */
    @PutMapping("/update/status/{id}")
    public ResponseData<?> updateStatus(@PathVariable Long id) {
        brandService.updateStatus(id);
        return ResponseData.success();
    }

    /**
     * 删除品牌
     */
    @DeleteMapping("/delete/{id}")
    public ResponseData<?> delete(@PathVariable Long id) {
        brandService.removeById(id);
        //log.info("删除品牌，需要刷新缓存");
        //stringRedisTemplate.convertAndSend(RedisChannelEnum.BRAND_REFRESH_CHANNEL.getChannel(), RedisKeyConstant.REFRESH);
        //log.info("缓存已经删除");
        return ResponseData.success();
    }

    /**
     * 获取品牌列表
     */
    @GetMapping("/list")
    public ResponseData<IPage<BrandVO>> list(PageParam<Brand> page, BrandParam param) {
        IPage<BrandVO> brandVOIPage = brandService.selectPage(page, param);
        return ResponseData.success("", brandVOIPage);
    }

    /**
     * 查看品牌详情并添加历史记录逻辑
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ResponseData<?> get(@PathVariable Long id) {
//        SysUserInfo sysUserInfo = SysUserContext.get();
        Brand one = brandService.getOne(new LambdaQueryWrapper<Brand>().eq(Brand::getId, id));
//        log.info("当前登录用户：" + SysUserContext.getUserId());
//        threadPoolTaskExecutor.execute(()->{
//            BrandProducterMsg msg = new BrandProducterMsg();
//            msg.setBrandId(id);
//            msg.setUserId(sysUserInfo.getId());
//            rabbitmqProducer.send(RabbitmqExchange.BRAND_EXCHANGE, BrandRoutingKey.BRAND_STATUS, msg);
//            log.info("添加历史记录");
//            log.info("添加成功");
//        });
//        stringRedisTemplate.expire(RedisKeyConstant.HISTORY_BRAND + SysUserContext.getUserId(),60, TimeUnit.SECONDS);
        return ResponseData.success("",one);
    }

    @GetMapping("/history")
    public ResponseData<?> history(PageParam<BrandHistoryVO> page) {
        IPage<BrandHistoryVO> pageList =  brandService.getHistory(page);
        return ResponseData.success("", pageList);
    }
}
